package zhang.lao.console.service;/**
 * Created by zhangqin on 2017/10/11.
 */

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.MapTool;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.console.constant.ConsoleUserConstant;
import zhang.lao.dao.base.*;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 自动驾驶分割
 *
 * @author
 * @create 2017-10-11 11:40
 **/
@Service
public class AutoMachineSplitService {


    @Autowired
    private WnMachineDeviceActiveDao wnMachineDeviceActiveDao;

    @Autowired
    private WnLnglatAutomaticDao wnLnglatAutomaticDao;

    @Autowired
    private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;

    @Autowired
    private WnMachineWorkReportAutoDao wnMachineWorkReportAutoDao;

    @Transactional
    public HttpResult split(String date){
        WnMachineWorkReportAutoExample wnMachineWorkReportAutoExample = new WnMachineWorkReportAutoExample();
        wnMachineWorkReportAutoExample.createCriteria().andWorkDateEqualTo(date);
        if(wnMachineWorkReportAutoDao.countByExample(wnMachineWorkReportAutoExample)==0) {
            Date s = DateUtil.StringToDate(date + " 00:00:00");
            Date e = DateUtil.StringToDate(date + " 23:59:59");
            WnMachineDeviceActiveExample wnMachineDeviceExample = new WnMachineDeviceActiveExample();
            wnMachineDeviceExample.createCriteria().andCreateDateEqualTo(DateUtil.StringToDate(date));
            List<WnMachineDeviceActive> wnMachineDeviceList = wnMachineDeviceActiveDao.selectByExample(wnMachineDeviceExample);
            List<Map<String, Date>> dates = Lists.newArrayList();
            if (wnMachineDeviceList != null) {
                for (WnMachineDeviceActive wnMachineDeviceActive : wnMachineDeviceList) {
                    final Integer deviceId = wnMachineDeviceActive.getDeviceId();
                    WnLnglatAutomaticExample wnLnglatAutomaticExample = new WnLnglatAutomaticExample();
                    wnLnglatAutomaticExample.createCriteria().andControlTypeEqualTo("1").andDeviceIdEqualTo(deviceId).andCreateTimeGreaterThan(s).andCreateTimeLessThan(e);
                    wnLnglatAutomaticExample.setOrderByClause("create_time asc");
                    List<WnLnglatAutomatic> lnglatAutomatics = wnLnglatAutomaticDao.selectByExample(wnLnglatAutomaticExample);
                    Date st = null;
                    Date en = null;
                    double length = 0.00;
                    boolean init = true;
                    int changeTime = 0;
                    for (int i = 0; i < lnglatAutomatics.size(); i++) {

                        WnLnglatAutomatic wnLnglatAutomatic = lnglatAutomatics.get(i);
                        Date createTime = wnLnglatAutomatic.getCreateTime();
                        if (init) {
                            st = createTime;
                            init=false;
                        }
                        WnLnglatAutomatic wnLnglatAutomatic2 ;
                        if(i+1 == lnglatAutomatics.size()){
                            wnLnglatAutomatic2 = lnglatAutomatics.get(i );
                        }else{
                            wnLnglatAutomatic2 = lnglatAutomatics.get(i+1 );
                        }

                        Date createTime2 = wnLnglatAutomatic2.getCreateTime();
                        long l = createTime2.getTime() - createTime.getTime();

                        if (l > 15000|| (changeTime==0&&i+1==lnglatAutomatics.size())) {
                            changeTime++;
                            en = createTime;
                            init = true;
                            length = 0.00;
                            if (st != null) {
                                Double workWidth = lnglatAutomatics.get(i).getWorkWidth();
                                double v = length * workWidth * 1000 * 0.0015;
                                Map<String, Date> map = Maps.newHashMap();
                                map.put("s", st);
                                map.put("e", en);
                                dates.add(map);
                                WnMachineWorkReportAuto wnMachineWorkReportAuto = new WnMachineWorkReportAuto();
                                wnMachineWorkReportAuto.setDeviceId(deviceId);
                                WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
                                wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(deviceId);
                                WnMachineDeviceLink first = ListUtils.getFirst(wnMachineDeviceLinkDao.selectByExample(wnMachineDeviceLinkExample));
                                if (first != null) {
                                    wnMachineWorkReportAuto.setMachineId(first.getMachineId());
                                }
                                wnMachineWorkReportAuto.setWorkArea(v);
                                wnMachineWorkReportAuto.setEndTime(en);
                                wnMachineWorkReportAuto.setStartTime(st);
                                wnMachineWorkReportAuto.setFarmId(wnMachineDeviceActive.getFarmId());
                                wnMachineWorkReportAuto.setWorkBreadth(workWidth);
                                wnMachineWorkReportAuto.setWorkDate(date);
                                wnMachineWorkReportAuto.setId(UUIDTool.getUUID());
                                wnMachineWorkReportAutoDao.insertSelective(wnMachineWorkReportAuto);
                            }

                        } else {
                            double getdistance = MapTool.getdistance(wnLnglatAutomatic.getLat(), wnLnglatAutomatic.getLng(), wnLnglatAutomatic2.getLat(), wnLnglatAutomatic2.getLng());
                            length += getdistance;
                        }
                        i = i + 2;
                    }

                }
            }
            return HttpResultUtil.buildSuccess();
        }else{
            return HttpResultUtil.buildSuccess();
        }
    }
}
